package com.taxshare.seller.util;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;

/**
 * @Auther: luozy
 * @Date: 2018/11/19 16:00
 * @Description:
 */
public class Xml2Map {
    private Xml2Map(){

    }
    public static Map<String, Object> Xml2Mapz(String xml)
            throws DocumentException {
        if (xml == null || "".equals(xml)) {
            return null;
        }
        SAXReader reader = new SAXReader();
        StringReader sr = new StringReader(xml);
        Document document = reader.read(sr);
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("UTF-8");
        Element root = document.getRootElement();
        return dom2Map(root);
    }

    private static Map dom2Map(Element e) {
        Map map = new HashMap();
        List list = e.elements();
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                Element iter = (Element) list.get(i);
                List mapList = new ArrayList();
                if (iter.elements().size() > 0) {
                    Map m = dom2Map(iter);
                    getTwoMap(map,iter,mapList,m);

                } else {
                    getNextMap(map,iter,mapList);
                }
            }
        } else{
            map.put(e.getName(), e.getText());
        }

        return map;
    }

    public static Map getTwoMap(Map map,Element iter,List mapList,Map m){
        if (map.get(iter.getName()) != null) {
            Object obj = map.get(iter.getName());
            if (!(obj instanceof ArrayList)) {
                mapList = new ArrayList();
                mapList.add(obj);
                mapList.add(m);
            }
            if (obj instanceof ArrayList) {
                mapList = (List) obj;
                mapList.add(m);
            }
            map.put(iter.getName(), mapList);
        } else{
            map.put(iter.getName(), m);
        }
        return map;
    }


    public static Map getNextMap(Map map,Element iter,List mapList){
        if (map.get(iter.getName()) != null) {
            Object obj = map.get(iter.getName());
            if (!(obj instanceof ArrayList)) {
                mapList = new ArrayList();
                mapList.add(obj);
                mapList.add(iter.getText());
            }
            if (obj instanceof ArrayList) {
                mapList = (List) obj;
                mapList.add(iter.getText());
            }
            map.put(iter.getName(), mapList);
        } else{
            map.put(iter.getName(), iter.getText());
        }
        return map;
    }

    public static Map<String, Object> getNewMapWithMapKey(
            Map<String, String> mappingMap, Map<String, Object> dataMap) {
        Map<String, Object> returnMap = new HashMap();
        Set keySet = mappingMap.keySet();
        Iterator iterator = keySet.iterator();
        while (iterator.hasNext()) {
            String key = iterator.next() + "";
            returnMap.put(key, dataMap.get(mappingMap.get(key)));
        }
        return returnMap;
    }

}